<template>
  <div class="header">
    <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
      <router-link to='/' class="navbar-brand">知乎专栏</router-link>
      <search-component class="list-inline mb-0">
        </search-component>
        <ul v-if="!user.isLogin" class="list-inline mb-0">
          <li class="list-inline-item">
            <router-link class="btn btn-outline-light my-2" to="/login">登录</router-link>
          </li>
          <li class="list-inline-item">
            <router-link class="btn btn-outline-light my-2" to="/register">注册</router-link>
          </li>
        </ul>
        <ul v-else class="list-inline mb-0">
          <li class="list-inline-item">
            <Dropdown :title="`你好 ${ user.name }`">
              <dropdown-item :disabled="false">
                <router-link to="/post" class="dropdown-item">新建文章</router-link>
              </dropdown-item>
              <dropdown-item><a href="#" class="dropdown-item">编辑文章</a></dropdown-item>
              <dropdown-item><a href="#" class="dropdown-item" @click="logout()">退出登录</a></dropdown-item>
            </Dropdown>
          </li>
        </ul>
    </nav>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, computed } from 'vue'
import Dropdown from './Dropdown.vue'
import DropdownItem from './DropdownItem.vue'
import { UserProps } from '@/hooks/testData'
import SearchComponent from './SearchComponent.vue'
import { useStore } from 'vuex'
export default defineComponent({
    name: "GlobalHeader",
    props: {
        user: {
            type: Object as PropType<UserProps>,
            required: true
        }
    },
    components: { Dropdown, DropdownItem, SearchComponent },
    setup() {
      const store = useStore()
      const logout = () => {
        sessionStorage.removeItem('TOKEN')
        store.commit('logout')
      }
      return {
        logout
      }
    }
})
</script>

<style scoped lang="less">

</style>
